Skip to content

Cortex-M backend: Refactor ConvertToCortexMPass#20070

Merged
AdrianLundell merged 2 commits into
pytorch:mainfrom
AdrianLundell:change-1257511
Jun 9, 2026
Merged

Cortex-M backend: Refactor ConvertToCortexMPass#20070
AdrianLundell merged 2 commits into
pytorch:mainfrom
AdrianLundell:change-1257511

Conversation

@AdrianLundell

@AdrianLundell AdrianLundell commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Change to use the general AtenToDialectPass structure, quantized_op_fusion_pass
to be changed similarly in an upcoming PR.

Removes the 1-1 ensure check as it was to restrictive, cortex-m backend needs to both insert scratch nodes and for BMM an additional transpose at the time of dialect replacement.

Bonus small fix: Moves yolo import into test to avoid download at test collection time

cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani

Change to use the general AtenToDialectPass structure.

Removes the 1-1 ensure check as it was to restrictive,
cortex-m backend needs to both insert scratch nodes and
for BMM an additional transpose at the time of dialect
replacement.

Bonus small fix: Moves yolo import into test to avoid download
at test collection time

Signed-off-by: Adrian Lundell <adrian.lundell@arm.com>
Change-Id: Ic62964522704d31f75c653bc265bcce02ce375c6
Copilot AI review requested due to automatic review settings June 5, 2026 14:49
@AdrianLundell AdrianLundell added the partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm label Jun 5, 2026
@AdrianLundell AdrianLundell requested a review from rascani as a code owner June 5, 2026 14:49
@AdrianLundell AdrianLundell added ciflow/trunk release notes: none Do not include this in the release notes labels Jun 5, 2026
@pytorch-bot

pytorch-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20070

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures

As of commit d46edf4 with merge base e56be3e (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Cortex-M backend’s ATen→dialect lowering to use the shared AtenToDialectPass substitution/decorator structure (replacing ConvertToCortexMPass), and relaxes the prior 1:1 “call_function count preserved” postcondition to allow scratch/aux node insertion (needed for CMSIS-NN scratch buffers and BMM transpose handling). Also adjusts the YOLO test to avoid triggering weights download during test collection.

Changes:

  • Remove the requires()/ensures() “call_function count preserved” check from AtenToDialectPass and delete the corresponding test.
  • Introduce AtenToCortexMPass and wire it into the Cortex-M pass manager in place of ConvertToCortexMPass.
  • Move YOLO model instantiation/weights loading into the test body to avoid collection-time side effects.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
backends/transforms/test/test_aten_to_dialect_pass.py Removes the test that asserted the old 1:1 call_function count invariant.
backends/transforms/aten_to_dialect_pass.py Removes the 1:1 substitution invariant enforcement (requires/ensures).
backends/cortex_m/test/models/test_yolo11.py Defers YOLO weights/model creation to test runtime to avoid collection-time downloads.
backends/cortex_m/passes/cortex_m_pass_manager.py Switches the pass pipeline to use AtenToCortexMPass.
backends/cortex_m/passes/aten_to_cortex_m_pass.py New pass implementing Cortex-M dialect substitutions + scratch alloc sizing logic.
backends/cortex_m/passes/init.py Updates public pass imports to expose AtenToCortexMPass.
Comments suppressed due to low confidence (1)

backends/cortex_m/test/models/test_yolo11.py:28

  • McuTestCase.model is typed as torch.nn.Module, but this test passes None with a type-ignore. This can hide real issues and could break if future test helpers start using test_case.model. Use a lightweight placeholder module instead so the dataclass contract stays valid.
    "yolo11n": McuTestCase(
        model=None,  # type: ignore[arg-type]
        example_inputs=lambda: (
            torch.randn(1, 3, 640, 640).to(memory_format=torch.channels_last),
        ),
    ),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backends/cortex_m/passes/aten_to_cortex_m_pass.py

@rascani rascani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update backends/cortex_m/passes/BUCK?

Comment thread backends/cortex_m/passes/cortex_m_pass_manager.py
Comment thread backends/transforms/aten_to_dialect_pass.py Outdated
Signed-off-by: Adrian Lundell <adrian.lundell@arm.com>
Change-Id: Ida9d298a0bf57c18957fb9117244adf260aadc12
@AdrianLundell

Copy link
Copy Markdown
Collaborator Author

Can you also update backends/cortex_m/passes/BUCK?

Done, can you verify that it looks correct?

@rascani

rascani commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Can you also update backends/cortex_m/passes/BUCK?

Done, can you verify that it looks correct?

LGTM, thanks for doing that.

@AdrianLundell AdrianLundell merged commit 0d5192c into pytorch:main Jun 9, 2026
475 of 478 checks passed
rascani added a commit to rascani/executorch that referenced this pull request Jun 9, 2026
Adapt _get_avg_pool2d_replacement from the old ConvertToCortexMPass class
to a @register_dialect_substitution function on AtenToCortexMPass,
matching the pattern established by pytorch#20070. Add casts to satisfy mypy's
node.args union type.

Add a structural assertion to test_dialect_avg_pool2d that verifies the
scratch buffer size matches cmsis_nn.avgpool_buffer_size() for the given
input/output dimensions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants